package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.jd.pojo.GoodsOrderStatus;
import com.gxa.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class OrderStatusServiceImpl implements OrderStatusService {

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<GoodsOrderStatus> findAll() {
        return orderStatusMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<GoodsOrderStatus> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<GoodsOrderStatus> orderStatuss = (Page<GoodsOrderStatus>) orderStatusMapper.selectAll();
        return new PageResult<GoodsOrderStatus>(orderStatuss.getTotal(), orderStatuss.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<GoodsOrderStatus> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return orderStatusMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<GoodsOrderStatus> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<GoodsOrderStatus> orderStatuss = (Page<GoodsOrderStatus>) orderStatusMapper.selectByExample(example);
        return new PageResult<GoodsOrderStatus>(orderStatuss.getTotal(), orderStatuss.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param
     * @return
     */
    @Override
    public GoodsOrderStatus findById(String orderId) {
        return orderStatusMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 新增
     *
     * @param orderStatus
     */
    @Override
    public void add(GoodsOrderStatus orderStatus) {
        orderStatusMapper.insert(orderStatus);
    }

    /**
     * 修改
     *
     * @param orderStatus
     */
    @Override
    public void update(GoodsOrderStatus orderStatus) {
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
    }

    /**
     * 删除
     *
     * @param
     */
    @Override
    public void delete(String orderId) {
        orderStatusMapper.deleteByPrimaryKey(orderId);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(GoodsOrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // order_id
            if (searchMap.get("orderId") != null && !"".equals(searchMap.get("orderId"))) {
                criteria.andLike("orderId", "%" + searchMap.get("orderId") + "%");
            }

            // status
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }

        }
        return example;
    }

}
